<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPerm('0',1)">
            <i class="el-icon-plus" /> 添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="PermList" row-key="id" default-expand-all>
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template #default="{row}">
            <el-button
              v-if="row.type===1"
              type="text"
              @click="addPerm(row.id,2)"
            >添加</el-button>
            <el-button type="text" @click="editperm(row.id)">编辑</el-button>
            <el-button type="text" @click="delpermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 弹窗 -->
      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="`${showText}权限点`" :visible="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermList, delpermission, addpermission, getpermDetail, editpermission } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      // 初始数据列表
      PermList: [],
      // 添加弹窗显示
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', //  添加到哪个节点下
        enVisible: '1' // 开启
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    // 获取初始数据
    this.getPermList()
  },
  methods: {
    // 发送初始数据请求
    async  getPermList() {
      this.PermList = listToTreeData(await getPermList(), '0')
      console.log(this.PermList)
    },
    // 删除权限点
    async delpermission(id) {
      // 二次确认
      await this.$confirm('确定要删除吗')
      // 发送请求
      await delpermission(id)
      // 提示用户
      this.$message.success('删除成功')
      // 更新页面
      this.getPermList()
    },
    // 添加按钮显示弹窗
    addPerm(id, type) {
      this.formData.pid = id
      this.formData.type = type
      this.showDialog = true
    },
    // 编辑功能
    async  editperm(id) {
      // 数据回显
      this.formData = await getpermDetail(id)
      // 显示弹窗
      this.showDialog = true
    },

    // 确认按钮
    async btnOK() {
      // 表单校验
      await this.$refs.perForm.validate()
      // 发送请求
      if (this.formData.id) {
        // 编辑
        await editpermission(this.formData)
      } else {
        // 新增
        await addpermission(this.formData)
      }

      // 提示用户
      this.$message.success('操作成功')
      // 更新页面
      this.getPermList()
      // 关闭弹窗
      this.showDialog = false
    },
    // 取消按钮
    btnCancel() {
      // 清空内容
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '1'
      }
      // 清空报错
      this.$refs.perForm.resetFields()
      // 关闭弹窗
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
